package com.gitee.feizns.explore.data_structure.array.easy;

import java.util.Arrays;

/**
 * 面试题 10.01. 合并排序的数组
 * @author feizns
 * @since 2020/3/3
 */
public class MergeArray {

    public static void main(String[] args) {
        int[] A = new int[]{ 1, 2, 3, 0, 0, 0 };
        int[] B = new int[]{ 4, 5, 6 };
        merge(A, 3, B, 3);
        System.out.println(Arrays.toString(B));
    }

    public static void merge(int[] A, int m, int[] B, int n) {
        int ai = m - 1;
        int bi = n - 1;
        for (int i = A.length - 1; i >= 0; i--) {
            if ( ai == -1 )
                A[i] = B[bi--];
            else if ( bi == -1 )
                A[i] = A[ai--];
            else if ( A[ai] > B[bi] )
                A[i] = A[ai--];
            else
                A[i] = B[bi--];
        }
    }

}
